//
//  ViewController.m
//  TGStreamSDK2
//
//  Created by Angelo on 3/17/15.
//  Copyright (c) 2015 com.neurosky. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupButtons];
    

    
    TGStreamInstance=[TGStream sharedInstance];
    TGStreamInstance.delegate=self;
    
    [TGStreamInstance enableLog:true];
    
    TextViewString=[NSString stringWithFormat:@"Stream SDK Log \n\nStream Version: %@\n\n",[TGStreamInstance getVersion]];
    _TextView.font=[UIFont fontWithName:@"Optima" size:14];
    _TextView.text=TextViewString;
    
}

#pragma mark -
-(void) setupButtons{
    
    NSArray *ButtonsArray=[NSArray arrayWithObjects:
                           _recordEnd,
                           _recordBegin,
                           _InitFile,
                           _InitBT,
                           _TearDown,
                           nil];
    
    for (UIButton *button in ButtonsArray)
    {
        button.layer.borderColor=[UIColor blueColor].CGColor;
        button.layer.borderWidth=2;
        button.titleLabel.font=[UIFont fontWithName:@"Optima" size:14];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    
}


-(NSString *) NowString{
    
    NSDate *date=[NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *DateString = [dateFormatter stringFromDate:date];
    
    return  DateString;
}


#pragma mark -
-(void) onDataReceived:(NSInteger)datatype data:(int)data obj:(NSObject *)obj deviceType:(DEVICE_TYPE)deviceType{
    
   NSLog(@"dataType:%d data:%d deviceType:%d",(int)datatype,data,(int)deviceType);
        
    if (obj) {
    
        TGSEEGPower *TGSEEGPowerInstance=(TGSEEGPower *)obj;
        NSString *TGSEEGPowerString=[NSString stringWithFormat:@"%@\n EEGPower delta-- %d\n EEGPower theta-- %d\n EEGPower lowAlpha-- %d\n EEGPower highAlpha-- %d\n EEGPower lowBeta-- %d\n EEGPower highAlpha-- %d\n EEGPower lowGamma-- %d\n EEGPower lowGamma-- %d\n ",
                                  [self NowString],
                                  TGSEEGPowerInstance.delta,
                                  TGSEEGPowerInstance.theta,
                                  TGSEEGPowerInstance.lowAlpha,
                                  TGSEEGPowerInstance.highAlpha,
                                  TGSEEGPowerInstance.lowBeta,
                                  TGSEEGPowerInstance.highAlpha,
                                  TGSEEGPowerInstance.lowGamma,
                                  TGSEEGPowerInstance.lowGamma];
        NSLog(@"%@",TGSEEGPowerString);

    }
    
}

static NSUInteger checkSum=0;

-(void) onChecksumFail:(Byte *)payload length:(NSUInteger)length checksum:(NSInteger)checksum{
    
    checkSum++;
    NSString *ChecksumFail=[NSString stringWithFormat:@"%@\n Check sum Fail:%lu\n",[self NowString],(unsigned long)checkSum];
    TextViewString=[TextViewString stringByAppendingString:ChecksumFail];
    [self updateTextView];
    
    NSLog(@"CheckSum lentgh:%lu  CheckSum:%lu",(unsigned long)length,(unsigned long)checksum);
}

-(void)onStatesChanged:(ConnectionStates)connectionState{
    
    NSString *StatesChanged=[NSString stringWithFormat:@"%@\n Connection States:%lu\n",[self NowString],(unsigned long)connectionState];
    TextViewString=[TextViewString stringByAppendingString:StatesChanged];
    NSLog(@"Connection States:%lu",(unsigned long)connectionState);
    
    if (connectionState== 4 || connectionState== 100 || connectionState== 101) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
        [_indicatorView stopAnimating];
            
        });

    }
    [self updateTextView];

}

-(void) onRecordFail:(RecrodError)flag{
    
    NSString *RecordFail=[NSString stringWithFormat:@"%@\n Record Fail:%lu\n",[self NowString],(unsigned long)flag];
    TextViewString=[TextViewString stringByAppendingString:RecordFail];
    [self updateTextView];
    
}

-(void) updateTextView{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        _TextView.text=TextViewString;
        
    });
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -
- (IBAction)recordBegin:(id)sender {
    
    [TGStreamInstance setRecordStreamFilePath];
    [TGStreamInstance startRecordRawData];
    
    
}

- (IBAction)recordEnd:(id)sender {
    
    [TGStreamInstance stopRecordRawData];
}

- (IBAction)InitFile:(id)sender {
    checkSum=0;
    
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *filePath = [mainBundle pathForResource:@"sample_data" ofType:@"txt"];
    
    [TGStreamInstance initConnectWithFile:filePath];
    
    [_indicatorView startAnimating];
}

- (IBAction)InitBT:(id)sender {
    
    [TGStreamInstance initConnectWithAccessorySession];
    [_indicatorView startAnimating];

}

- (IBAction)TearDown:(id)sender {
    
    [TGStreamInstance tearDownAccessorySession];
    
}
@end
